#-*- coding: utf-8 -*-

#This file is part of pyLot library.
#
# pyLot is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pyLot is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with pyLot.  If not, see <http://www.gnu.org/licenses/>.

__author__ = u'Pierre Puiseux, Guillaume Baty'
__copyright__ = u"Copyright 2011-2012 (C) andheo, Université de Pau et des Pays de l'Adour"
__credits__ = [u'Pierre Puiseux', u'Guillaume Baty']
__license__ = "GNU Lesser General Public License"

__all__ = ['DataConverterPlugin']

from traits.api import List
from envisage.api import Plugin

from .interfaces.api import *

class DataConverterPlugin(Plugin) :

  ext_service_offers = List(contributes_to='envisage.service_offers')
  ext_formats = List(contributes_to='dataconverter.formats')
  ext_readers = List(contributes_to='dataconverter.readers')
  ext_writers = List(contributes_to='dataconverter.writers')
  ext_reprs = List(contributes_to='dataconverter.reprs')
  ext_adapters = List(contributes_to='dataconverter.adapters')
  ext_converters = List(contributes_to='dataconverter.converters')
  ext_qt4viewers = List(contributes_to='dataconverter.qt4viewers')

  dataformats = []
  datareprs = []
  datareaders = []
  datawriters = []
  dataadapters = []
  dataconverters = []
#  qt4viewers = []

  def __init__(self, *args, **kargs):
    Plugin.__init__(self, *args, **kargs)

    self.plugin_types = [
      # datatype, (id,class) list, datatype_id, property names  
      (IDataFormat, self.dataformats, 'dataconverter.formats'),
      (IDataRepr, self.datareprs, 'dataconverter.reprs'),
      (IDataReader, self.datareaders, 'dataconverter.readers'),
      (IDataWriter, self.datawriters, 'dataconverter.writers'),
      (IDataAdapter, self.dataadapters, 'dataconverter.adapters'),
      (IDataConverter, self.dataconverters, 'dataconverter.converters'),
#      (IViewer, self.qt4viewers, 'dataconverter.qt4viewers'),
      ]


    self._datas = {}
    for dtype, lst, identifier in self.plugin_types :
      self._datas[identifier] = {}
      for obj in lst :
        if len(obj) == 3 :
          fmt_id, fmt, props = obj
        elif len(obj) == 2 :
          fmt_id, fmt = obj
        self._datas[identifier][fmt_id] = fmt

#  def _service_offers_default(self):
#    services = []
#    for datatype, lst, datatype_id in self.plugin_types :
#      for obj in lst :
#        if len(obj) == 3 :
#          fmt_id, fmt, props = obj
#        elif len(obj) == 2 :
#          fmt_id, fmt = obj
#          props = {}
#
#        properties = dict(
#          identifier=fmt_id,
#          datatype=datatype,
#          datatype_id=datatype_id
#          )
#        properties.update(props)
#
#        service = ServiceOffer(
#            protocol=datatype,
#            factory=self._create,
#            properties=properties
#            )
#
#        services.append(service)
#    return services

#  def _create(self, *args, **kargs):
#    if not kargs : return None
#    if kargs['datatype'] == IDataReader :
#      dataformat = self.application.get_service(
#        IDataFormat, 'identifier=="%s"' % kargs['dataformat_id'])
#      datarepr = self.application.get_service(
#        IDataRepr, 'identifier=="%s"' % kargs['datarepr_id'])
#      obj = self._datas[kargs['datatype_id']][kargs['identifier']](dataformat, datarepr)
#    else :
#      obj = self._datas[kargs['datatype_id']][kargs['identifier']]()
#    return obj

  def _ext_formats_default(self):
    return self._datas['dataconverter.formats'].values()

  def _ext_readers_default(self):
    return self._datas['dataconverter.readers'].values()

  def _ext_reprs_default(self):
    return self._datas['dataconverter.reprs'].values()

  def _ext_writers_default(self):
    return self._datas['dataconverter.writers'].values()

  def _ext_adapters_default(self):
    return self._datas['dataconverter.adapters'].values()

  def _ext_converters_default(self):
    return self._datas['dataconverter.converters'].values()

#  def _ext_qt4viewers_default(self):
#    return self._datas['dataconverter.qt4viewers'].values()

if __name__ == '__main__' :

  plugin = DataConverterPlugin()
  print plugin.viewers
